package com.szly.phm.admin.controller;


import com.szly.phm.admin.aspectj.annotation.OperationLogAnnotation;
import com.szly.phm.admin.aspectj.enums.ModuleEnum;
import com.szly.phm.admin.aspectj.enums.OperationActionEnum;
import com.szly.phm.admin.utils.CustomResponse;
import com.szly.phm.admin.utils.SecurityUtils;
import com.szly.phm.common.entity.dto.role.*;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.role.RolePermissionVO;
import com.szly.phm.common.service.ISysPermissionService;
import com.szly.phm.common.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-07-02
 */
@RestController
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/pc/role")
@Api(tags = "角色管理")
public class SysRoleController {

    private final ISysRoleService roleService;

    private final ISysPermissionService sysPermissionService;

    @ApiOperation(value = "分页查询角色列表")
    @GetMapping("/v1/roles")
    @CustomResponse
    public RootResponse queryRolePageV1(@Valid RolePageDTO roleDTO) {
        return roleService.queryRolePage(roleDTO);
    }

    @ApiOperation(value = "添加角色")
    @OperationLogAnnotation(getModuleName = ModuleEnum.ROLE,getOperationAction = OperationActionEnum.SAVE)
    @PostMapping("/v1/roles")
    @CustomResponse
    public RootResponse createRoleV1(@Valid @RequestBody CreateRoleDTO roleDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return roleService.createRole(roleDTO,userDTO);
    }

    @ApiOperation(value = "修改角色")
    @CustomResponse
    @OperationLogAnnotation(getModuleName = ModuleEnum.ROLE,getOperationAction = OperationActionEnum.UPDATE)
    @PutMapping("/v1/roles")
    public RootResponse updateRoleV1(@Valid @RequestBody UpdateRoleDTO roleDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return roleService.updateRole(roleDTO,userDTO);
    }

    @ApiOperation(value = "删除角色")
    @OperationLogAnnotation(getModuleName = ModuleEnum.ROLE,getOperationAction = OperationActionEnum.DELETE)
    @ApiImplicitParam(name = "id", value = "角色Id", dataType = "String", required = true, dataTypeClass = String.class)
    @DeleteMapping("/v1/roles")
    @CustomResponse
    public RootResponse deleteRoleV1(@NotBlank(message = "角色Id不能为空") @RequestParam(value = "id") String id) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return roleService.deleteRole(id,userDTO);
    }

    @ApiOperation(value = "查询角色拥有的权限")
    @ApiImplicitParam(name = "roleId", value = "角色Id", dataType = "String", required = true, dataTypeClass = String.class)
    @GetMapping("/v1/roles/permissions")
    @CustomResponse
    public RootResponse<RolePermissionVO> queryRolePermissionsV1(@NotBlank(message = "角色Id不能为空") String roleId) {
        return roleService.queryRolePermissions(roleId);
    }


    @ApiOperation(value = "给角色分配权限")
    @OperationLogAnnotation(getModuleName = ModuleEnum.ROLE,getOperationAction = OperationActionEnum.SAVE)
    @PostMapping("/v1/roles/permissions")
    @CustomResponse
    public RootResponse createAssignPermissionsV1(@Valid @RequestBody RolePermissionDTO roleDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return roleService.createAssignPermissions(roleDTO,userDTO);
    }

    @ApiOperation(value = "查询赋权限树")
    @GetMapping("/v1/treeRolePermission")
    @CustomResponse
    public RootResponse reset() {
        return roleService.treePermission();
    }

    @ApiOperation(value = "多角色查询赋权限树")
    @GetMapping("/v1/treeRolesPermission")
    @CustomResponse
    public RootResponse resetV1( @RequestParam String[] roleIds) {
        //是否超级管理员
        AtomicBoolean supperAdmin = new AtomicBoolean(false);
        return RootResponse.success(sysPermissionService.queryPermissionsByRoleIds1(Arrays.asList(roleIds),  supperAdmin));
    }

}

